//
// Created by hunan on 25-4-6.
//
#include <flv_parser.h>
#include <string_util.h>
#include <gtest/gtest.h>
#include "file_module/file_handle.h"

TEST(Print, Test)
{
    printf("hello.\n");
}

TEST(FlvProtocolHeader, ParserHeader)
{
    std::string header = "09022dc100044d00000000";
    std::vector<uint8_t> data;
    StringUtil::HexStrToCharacter(header, data);
    FlvParser parser;
    FlvMessage::FlvTag tag = {};
    int use_length = 0;
    int status = parser.ParseFlvTagHeader(data.data(), data.size(), use_length, tag);
    printf("status : %d,use length : %d.\n", status, use_length);
    printf("tag type : %d.\n", tag.m_tag_type);
    printf("tag size : %d.\n", tag.m_data_size);
    printf("time stamp : %d.\n", tag.m_time_stamp);
    /*
    *
    *status : 2,use length : 11.
    tag type : 9.
    tag size : 142785.
    time stamp : 1101.
     */
}

TEST(FlvProtocolTag, ParserTag)
{
    std::string header = "09022dc100044d00000000";
    std::vector<uint8_t> data;
    StringUtil::HexStrToCharacter(header, data);
    FlvParser parser;
    FlvMessage::FlvTag tag = {};
    int use_length = 0;
    tag.m_data_size =  data.size();
    int status = parser.ParseFlvTagData(data.data(), data.size(), use_length, tag);
    printf("status : %d,use length : %d.\n", status, use_length);
}

TEST(FlvProtocolHeader, ParserFlvHeader)
{
    std::string header = "464c5601010000000900000000120001050000000000000002000a6f6e4d65746144617461080000000b00086475726174696f6e00402ab126e978d4fe0005776964746800408e0000000000000006686569676874004080e00000000000000d766964656f64617461726174650040686a000000000000096672616d657261746500403df853e2556b28000c766964656f636f6465636964004000000000000000000b6d616a6f725f6272616e640200046d703432000d6d696e6f725f76657273696f6e020001300011f623b4a3b523b3a3a1df7e74764c764c761c744be5b123afcf5fc75ec5bdcefafaf6fa31efe0867935d80fc4ae8a7bf9a0119c7b1df98f897dd68867ada0fd67c3becf67675f5e19f6fb9b4ed9d78e867dfad1da67b4dda07a1df7e89ecf3d9a7b2cf443e7cd3d967b24f631e897c5e51eca3d891d820d041c51f7b7e37832bf8195d1703400d64078f477cbdcf279f79032b4541958c00aab81949fc02dde3b152eac2ff457143ea2a4f4cba3f03e2428a770cf5a0931f7e7a3bdfd73d3e1e19ec2bfc0ae8b7a8f0f1df8b8b14f8bc5f3affde3b7e195b805463dedf5e5c2419a090f522af8eba72a0180f8f4af98fa900b7498e773d49f12c022a68933fa7aca8efee98f1713badf933e9a093ffde3a69d58abd63e7e7ebe64bfd0c39f9f398979a99a06f9cbcb4c4bd0b7eb5e865ad9efd37c33ed3629edb3dadad0cfb2ed6cec6c43d0cfbf635faeed339a58e877dc6a1ed33d9fa219c3beaf54d66df828b9fc33aba73768feb4621f0fbeb8d3f0937d5b3b865c0c039cb93f3a49bbb7ea7a26aa8f7c3eead25966bacb37c0c039df6486aec4a127c3f9c6c9a4be5d5cef1f1d9da1fb5b59675d5a9fb5b6a7edf6ba55d95da5d896fb81ebaebf22b3528e767e3067a03d3b7eac7c7330bd51e8cf573dbd46640cf2c7b4fea691f893cf8bc6f7cfe77cde81fce8f8a3cf7f3e1ccf8af1f9afd6fc7c4be3f1c180f6d623f18180f758dec79fa1e278b50e7b3ebc82f563d24c99cc6cfe7a93b99292c59e7b7de170fe5994ddc81f9a7b2c794fe3c54f8faa9cf27d12d2d6ed52be6537692b5cecded7147e3ed2f3f696f5d29ea010ecf6981e12a4c39700116d6d297f5eec79f8224974be25d5560157219e3ab55af8e5419bcfdcab6c0236f47e015ac0110f1ef9ada3db7aa011b4f877dbee6b865b9ac19433ecf4f47473a21f7e6bb24f653b163a25f3e39ec8aec23d88f8a7bd659accc97e4c3d14f6bbe12bdcd1a5c587ef7c01772013254dbbbf0d5aa84fa4acde08970de8d87e30029edfad87edea0046b00443befd93db75da87b500221df75b87b73541a083d8008877ddedeb3b5e0056a0f1f0efbf45d96ed19e0cf38ec43e2f19d82ec8f1fc2c7c5bdeb37d79867efdd18f6e6f2faf0f5d40ca38fbd1e7df1ec0aeb58195bd00a900683f5e0f3f16bb32baee80560fc02a32066f4797a0fd3aec8f805683e20fbe4f575b53521df71aa7b4cf696779d0efb6cf819659ec88ec6897dc649ec68ec6c53f14f6bc48ec08a8fe21fb71f4331bfb8e7a6971e06801abe173abaaa5bff7bb4745551d55535334e73a9a8a9a89b739d45454544db9b8abffa7dd680a01a53d5fc9a63df3ce95caaeabaf6c7e0157161e90819bcaf06bb08022f0f5d004461f7a4d7be75a8656d1d691cf7a7d7a9c0caf55d3b6b5f787a3fce797bef432ad12a13ddc87ab95587a4002de9cdaa2d1dc3d3cea79e971f3f32016e8194e6e6a726e7dc273331353540df8cbfbb4be731f33fe3f0efbad73fafa9402b5943e19f6db55fb6ed9a196e42beef5b50febbb5df0cfbf4a067a6fd83fafa7e1df7ec9fe0be3bac7b6bee867dcefefef9edc507e19f7f01fdcf7feabb1fba011b1032867d7578108bc0f977bf5b336dae0865e2551d53d802404383f805ccc82b554dbc47cedadadede55d945d7c32bd872dee4f499f777dcec7e197aa3f727ae8f494a79dcf3f3be0159de068012f478ebd06820e381a0fc7a0087e58341098c5c5de7dd8045e8f0ec78341faf279f880d0426617834009747afd58651f002de4f2f56865a67f2e80566834109127df1fa8773c02b2a0340098901a0058ba19bd9e6d845de009ba06820eebde3d6d27ff9d778e22ba8ab1f6d6d6d2aec7a76b6b6b6d2ceb3b5b6b6b696758ee2ea8656d6f2aece2fe83400b8d41a006b7b9ae92";
    std::vector<uint8_t> data;
    StringUtil::HexStrToCharacter(header, data);
    FlvParser parser = {};
    int use_length = 0;
    int status = parser.ParseFlvHeader(data.data(), data.size(), use_length);
    printf("status : %d,use length : %d.\n", status, use_length);
    auto flv_header = parser.GetFlvHeader();
    printf("signature : %s.\n", flv_header.m_signature.c_str());
    printf("version   : %d.\n", flv_header.m_version);
    printf("audio flag: %d.\n", flv_header.m_audio_flag);
    printf("video flag: %d.\n", flv_header.m_video_flag);
    printf("data offset:%d.\n", flv_header.m_data_offset);
}

TEST(FlvProtocol, ParserFlvMessage)
{
    std::string header = "464c5601010000000900000000120001050000000000000002000a6f6e4d65746144617461080000000b00086475726174696f6e00402ab126e978d4fe0005776964746800408e0000000000000006686569676874004080e00000000000000d766964656f64617461726174650040686a000000000000096672616d657261746500403df853e2556b28000c766964656f636f6465636964004000000000000000000b6d616a6f725f6272616e640200046d703432000d6d696e6f725f76657273696f6e020001300011636f6d70617469626c655f6272616e64730200106d7034326d70343169736f6d617663310007656e636f64657202000d4c61766635372e38332e313030000866696c6573697a65004128d98200000000000009000001100900c09200000000000000120000840081e0010e12176361fb23d76a81047df6c7447653f2cd94fc9e586ddd772ac7ccbaf41a083baf8923cff7a19605127d6f0d484009795df9ebf7e051ffb0972be3fba723647bdbef3d479ca35b9f0fcee50301ec064ee48301ee5dbc3f1a0d041bd1f756129fafc280c039fbcc57da970ffc3ff290ca52aaae773f3afafb1b197768ebec1f61612ceca6c2c2d94d75a3a54f3ba8b2fc009bc1faa93c3a758d9574aca79ddf9147cfc9f12db7d56bb0be5df86a3a2b3ef2795670340f7aba25fba7750f4439f27a7a9a7ab0efbbd4d43dafafd48f877db6a1ed4008d03da110fabd6008d83d9d3c7f2603400b127df1fb07b5cf65506801b341a0848852e7c3cab58f6b6680467034107107df17a8ecb54ecd06820f28328a7be7c39e3f823c51d37887b076ba39e9f8df9a7747f0ee4dd3f9c19c5b9edf0f54f6ae96b1e887d9eb6bec6cc43edf636ebf6b6df0efb4dc3fbcfdedd867dfbe7b740236ddaf87e19f7f119e0008d906820f6002219f75be7b7b60f6d57433edb7002765dba7b6e19f55ebe78952e9cd406801324bb8f897be3ee47de670f5a8fe69eb6f2b8fbd2f1fc74a74025dcc2eec4ab7492d47f6a8b863101433492ee8ffd823f5f257d74fbc26cf57433d0f17d7650fc8b5e2a1d7c7e18f821f0edd51fc570f6c1fd2f179c8ca5ce7b3ebc4f0f9508b6749f4021d9b554689f4d5896636949f8a9e7be47b7b8a0fedee433ee3700235032d9d47433ed731d939d1f9912f9bc9c5764bb0dd14f9160bafafefa2ded5df648fdf81f7e5805c539f29a3033cc009d2d1887db67d0cf48fe93f561df62d57ebec6bc3bec2d7db7ecd7ee433ecbbbbbb7b47a19f77bbb4ed8008d5819433efd58ed43da31d9e7a1df7e9bb363b3cf663a21f6f978f1d958ae897c5e31ec3f065847af07ce8baf7b7dcc068012c3caa3ab950655e25d50015297faf773cbc0082af55e469d927f22f5f907a2e4c73dfc93f927724f639e897cd6279d8186ec33f14f79bebfc2009c13f16f6eefab4207e79d7caa5afab2e553987640bda3af0e6b3dea7e667a1c9b9e7b8ba7312f333140e1396959796a16f9cacacacad0b7a8d9d3faa5c10d59734fdbdb3d0c79f5f8b00360ff9ff612e4fe2ade0cb73aed8f2b7c4a890d3e1f5e5d0cb57fb1fa704bfbb4217c56dba24d3e7daa7774eea539b06e19f71b72d7690f849a257c7b3abbb6764f43bebabb12020f84b8b7c1a282b1d454de935e2a1f83020aabedaeb9c90565d869e6067258d8da4fcad3eca55da96a017681959594abb4ecac6cace59d63b1b4fc3f67672cece2fd5842009c7f891432b3b6a4b250bdef079f9a079fa03c7de7ffef3b0a5f5a76367e933d9e7e28f848944726ccf8943cbc357aa64357ffec3b1ca6e923d1e5e6bb3c33c7008d10ce29cf8fce2ef9dbb5433d1929bc21f2b61f1a016f6e7dcd0caec1a084b7009b8009933aee71f76079d7a0d0425ba9f13dbec7c9ebddce36c1a0d002e02a068016dcf5b490341faf07925c0f1d72a0eda5b4a9f777173550659625cc00bb5c708355692c8aaa75e1fbd0d13d9aa8ee3406801229f17a75dac7b2cbf8fd6ffcec439f2bb419ef6edb5fbb0cfb7dfdd76f6eba19f6fb5aaed9d6743becf4f4f534e1df7e99ed33d9eecd3d0efbf5402350f67004671e877dfaa6b465afce008ca55e90dc4be2f2557c33cc2f3f883c33814ba1e8dd73d6f474b6bb30bcde8be25f13687c7fbec33b55faefdb8873e5f736cfee6e3e19f71c15fbeee0de742becbbbb9b7b31d0cfbf623b4a3b523b3a3a1df7e74764c764c761c744be5b123afcf5fc75ec5";
    std::vector<uint8_t> data;
    StringUtil::HexStrToCharacter(header, data);
    FlvParser parser = {};
    int use_length = 0;
    int status = parser.ParseFlvHeader(data.data(), data.size(), use_length);
    printf("status : %d,use length : %d.\n", status, use_length);
    auto flv_header = parser.GetFlvHeader();
    printf("signature : %s.\n", flv_header.m_signature.c_str());
    printf("version   : %d.\n", flv_header.m_version);
    printf("audio flag: %d.\n", flv_header.m_audio_flag);
    printf("video flag: %d.\n", flv_header.m_video_flag);
    printf("data offset:%d.\n", flv_header.m_data_offset);
    int new_use_length = 0;
    status  = parser.ParseFlvTag(data.data() + use_length, data.size() - use_length, new_use_length);
    printf("status : %d,use length : %d.\n", status, new_use_length);

}

TEST(FlvProtocol, Parser)
{
    std::string header = "464c5601010000000900000000120001050000000000000002000a6f6e4d65746144617461080000000b00086475726174696f6e00402ab126e978d4fe0005776964746800408e0000000000000006686569676874004080e00000000000000d766964656f64617461726174650040686a000000000000096672616d657261746500403df853e2556b28000c766964656f636f6465636964004000000000000000000b6d616a6f725f6272616e640200046d703432000d6d696e6f725f76657273696f6e020001300011636f6d70617469626c655f6272616e64730200106d7034326d70343169736f6d617663310007656e636f64657202000d4c61766635372e38332e313030000866696c6573697a65004128d98200000000000009000001100900c09200000000000000120000840081e0010e12176361fb23d76a81047df6c7447653f2cd94fc9e586ddd772ac7ccbaf41a083baf8923cff7a19605127d6f0d484009795df9ebf7e051ffb0972be3fba723647bdbef3d479ca35b9f0fcee50301ec064ee48301ee5dbc3f1a0d041bd1f756129fafc280c039fbcc57da970ffc3ff290ca52aaae773f3afafb1b197768ebec1f61612ceca6c2c2d94d75a3a54f3ba8b2fc009bc1faa93c3a758d9574aca79ddf9147cfc9f12db7d56bb0be5df86a3a2b3ef2795670340f7aba25fba7750f4439f27a7a9a7ab0efbbd4d43dafafd48f877db6a1ed4008d03da110fabd6008d83d9d3c7f2603400b127df1fb07b5cf65506801b341a0848852e7c3cab58f6b6680467034107107df17a8ecb54ecd06820f28328a7be7c39e3f823c51d37887b076ba39e9f8df9a7747f0ee4dd3f9c19c5b9edf0f54f6ae96b1e887d9eb6bec6cc43edf636ebf6b6df0efb4dc3fbcfdedd867dfbe7b740236ddaf87e19f7f119e0008d906820f6002219f75be7b7b60f6d57433edb7002765dba7b6e19f55ebe78952e9cd406801324bb8f897be3ee47de670f5a8fe69eb6f2b8fbd2f1fc74a74025dcc2eec4ab7492d47f6a8b863101433492ee8ffd823f5f257d74fbc26cf57433d0f17d7650fc8b5e2a1d7c7e18f821f0edd51fc570f6c1fd2f179c8ca5ce7b3ebc4f0f9508b6749f4021d9b554689f4d5896636949f8a9e7be47b7b8a0fedee433ee3700235032d9d47433ed731d939d1f9912f9bc9c5764bb0dd14f9160bafafefa2ded5df648fdf81f7e5805c539f29a3033cc009d2d1887db67d0cf48fe93f561df62d57ebec6bc3bec2d7db7ecd7ee433ecbbbbbb7b47a19f77bbb4ed8008d5819433efd58ed43da31d9e7a1df7e9bb363b3cf663a21f6f978f1d958ae897c5e31ec3f065847af07ce8baf7b7dcc068012c3caa3ab950655e25d50015297faf773cbc0082af55e469d927f22f5f907a2e4c73dfc93f927724f639e897cd6279d8186ec33f14f79bebfc2009c13f16f6eefab4207e79d7caa5afab2e553987640bda3af0e6b3dea7e667a1c9b9e7b8ba7312f333140e1396959796a16f9cacacacad0b7a8d9d3faa5c10d59734fdbdb3d0c79f5f8b00360ff9ff612e4fe2ade0cb73aed8f2b7c4a890d3e1f5e5d0cb57fb1fa704bfbb4217c56dba24d3e7daa7774eea539b06e19f71b72d7690f849a257c7b3abbb6764f43bebabb12020f84b8b7c1a282b1d454de935e2a1f83020aabedaeb9c90565d869e6067258d8da4fcad3eca55da96a017681959594abb4ecac6cace59d63b1b4fc3f67672cece2fd5842009c7f891432b3b6a4b250bdef079f9a079fa03c7de7ffef3b0a5f5a76367e933d9e7e28f848944726ccf8943cbc357aa64357ffec3b1ca6e923d1e5e6bb3c33c7008d10ce29cf8fce2ef9dbb5433d1929bc21f2b61f1a016f6e7dcd0caec1a084b7009b8009933aee71f76079d7a0d0425ba9f13dbec7c9ebddce36c1a0d002e02a068016dcf5b490341faf07925c0f1d72a0eda5b4a9f777173550659625cc00bb5c708355692c8aaa75e1fbd0d13d9aa8ee3406801229f17a75dac7b2cbf8fd6ffcec439f2bb419ef6edb5fbb0cfb7dfdd76f6eba19f6fb5aaed9d6743becf4f4f534e1df7e99ed33d9eecd3d0efbf5402350f67004671e877dfaa6b465afce008ca55e90dc4be2f2557c33cc2f3f883c33814ba1e8dd73d6f474b6bb30bcde8be25f13687c7fbec33b55faefdb8873e5f736cfee6e3e19f71c15fbeee0de742becbbbb9b7b31d0cfbf623b4a3b523b3a3a1df7e74764c764c761c744be5b123afcf5fc75ec5";
    std::vector<uint8_t> data;
    StringUtil::HexStrToCharacter(header, data);
    FlvParser parser = {};
    int new_use_length = 0;
    int status  = parser.ParseFlvMessage(data.data(), data.size(), new_use_length);
    printf("status : %d,use length : %d.\n", status, new_use_length);
}

TEST(FlvProtocol, FlvFile)
{
    std::string file = "/home/hunan/download/nasa_03.flv";
    FileHandle handle = FileHandle(file, 100000);
    int result = handle.OpenFileOnlyRead();
    ASSERT_GE(result, 0);
    FlvParser parser = {};
    while (true)
    {
        int new_use_length = 0;
        int data_length = 0;
        uint8_t * data = handle.Data(data_length);
        if (!data || data_length == 0)
        {
            break;
        }
        ASSERT_GE(data_length, 1);
        int status = parser.ParseFlvMessage(data, data_length, new_use_length);
        //printf("status : %d,use length : %d.\n", status, new_use_length);
        if (status == FlvParser::FlvProtocolStatus::PARSER_BAD)
        {
            break;
        }
        handle.RemoveDataLength(new_use_length);
    }
}